package com.crdeng.business.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crdeng.business.entity.Consumer;
import com.crdeng.business.entity.OutStock;
import com.crdeng.business.service.ConsumerService;
import com.crdeng.business.service.OutStockService;
import com.crdeng.business.vo.*;
import com.crdeng.handler.BusinessException;
import com.crdeng.response.Result;
import com.crdeng.response.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

/**
 * <p>
 * 出库单接口
 * </p>
 *
 * @author crdeng
 * @since 2021-02-06
 */
@Api(value = "出库单", tags = "出库单接口")
@RestController
@RequestMapping("/business/out-stock")
public class OutStockController {

    @Autowired
    private OutStockService outStockService;
    @Autowired
    private ConsumerService consumerService;


    @ApiOperation(value = "出库单", notes = "条件查询出库单")
    @PostMapping("/findOutStock")
    public Result findOutStock(@RequestParam(defaultValue = "1") Integer pageNun,
                               @RequestParam(defaultValue = "6") Integer pageSize,
                               @RequestBody outStocksQueryMap queryMap) {
        IPage<OutStockVO> page = outStockService.findOutStock(pageNun, pageSize, queryMap);
        return Result.ok().data("list", page.getRecords()).data("total", page.getTotal());
    }

    /*订单明细*/
    @ApiOperation(value = "出库明细", tags = "根据ID查询出库明细")
    @GetMapping("/getDetail")
    public Result getDetail(@RequestParam Long id, @RequestParam(defaultValue = "1") Integer pageNun,
                            @RequestParam(defaultValue = "3") Integer pageSize) {
        OutStockDetailVO detailVO = outStockService.getDetail(id, pageNun, pageSize);
        return Result.ok().data("data", detailVO);
    }

    @ApiOperation(value = "删除出库单", tags = "删除出库单")
    @DeleteMapping("/delete")
    public Result delete(Long id) {
        OutStock byId = outStockService.getById(id);
        if (byId == null) {
            throw new BusinessException(ResultCode.PARAM_NOT_VALID.getCode(), "出库单不存在");
        }
        outStockService.removeById(id);
        return Result.ok();
    }


    @ApiOperation(value = "通过出库单", tags = "通过出库单")
    @GetMapping("/pass")
    public Result pass(Long id) {

        outStockService.alterStatus(id,0);
        return Result.ok();
    }

    /*恢复订单*/
    @ApiOperation(value = "恢复订单", tags = "恢复订单")
    @GetMapping("/recover")
    public Result recover(Long id) {
            outStockService.alterStatus(id,0);
        return Result.ok();
    }

  /*回收站*/
    @ApiOperation(value = "回收站", tags = "回收站")
    @GetMapping("/recycle")
    public Result recycle(Long id) {
            outStockService.alterStatus(id,1);
        return Result.ok();
    }

    @ApiOperation(value = "出库单",tags = "生成出库单")
    @PostMapping("/addOutStocks")
    public Result addOutStocks(@RequestBody OutStockVO outStockVO) {
        System.out.println(outStockVO);
        if(outStockVO.getConsumerId()==null){
            //说明现在添加物资来源
            ConsumerVO consumerVO = new ConsumerVO();
            BeanUtils.copyProperties(outStockVO,consumerVO);
            if("".equals(consumerVO.getName())||consumerVO.getName()==null){
                throw new BusinessException(ResultCode.PARAM_IS_BLANK.getCode(),"物资去向名不能为空");
            }
            if("".equals(consumerVO.getContact())||consumerVO.getContact()==null){
                throw new BusinessException(ResultCode.PARAM_IS_BLANK.getCode(),"联系人不能为空");
            }
            if("".equals(consumerVO.getAddress())||consumerVO.getAddress()==null){
                throw new BusinessException(ResultCode.PARAM_IS_BLANK.getCode(),"地址不能为空");
            }
            if("".equals(consumerVO.getPhone())||consumerVO.getPhone()==null){
                throw new BusinessException(ResultCode.PARAM_IS_BLANK.getCode(),"联系方式不能为空");
            }
            if(consumerVO.getSort()==null){
                throw new BusinessException(ResultCode.PARAM_IS_BLANK.getCode(),"排序不能为空");
            }
            Consumer consumer = consumerService.add(consumerVO);

            outStockVO.setConsumerId(consumer.getId());
        }
        //提交发放单
        outStockService.addOutStock(outStockVO);
        return  Result.ok();
    }


}

